package com.example.protestgps;

import java.util.ArrayList;
import java.util.List;


import android.R.string;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLiteforSMS extends SQLiteOpenHelper {

	// Database Version
	private static final int DATABASE_VERSION = 1;

	// Database Name
	private static final String DATABASE_NAME = "mydatabase1";

	// Table Name
	private static final String TABLE_DESPOINT = "deslocation";
	
//	public static final String  selectdata= "Select * from "+TABLE_DESPOINT;
	public String dataselect= "";
	public String latlng[] = new String[100];
	public int i = 0;


	public SQLiteforSMS(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_DESPOINT
				+ "(DES_ID INTEGER PRIMARY KEY," + "DES_LAT DOUBLE,"
				+ "DES_LNG DOUBLE," + "DES_MOBILENO TEXT(10));");

		Log.d("CREATE TABLE", "Create Table Successfully.");

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

	
	// Insert Data
	public long InsertData(int ID,double deslat, double deslng, String mobileno) {
		// TODO Auto-generated method stub

		try {
			SQLiteDatabase db;
			db = this.getWritableDatabase(); // Write Data
			ContentValues Val = new ContentValues();
			Val.put("DES_ID", ID);
			Val.put("DES_LAT", deslat);
			Val.put("DES_LNG", deslng);
			Log.d("deslng", "" + deslng);
			Val.put("DES_MOBILENO", mobileno);
			

			long rows = db.insert(TABLE_DESPOINT, null, Val);
			Log.d("Val", "" + Val);

			db.close();
			return rows; // return rows inserted.

		} catch (Exception e) {
			return -1;
		}

	}
	
	public List<Members> SelectAllData() {
		// TODO Auto-generated method stub
		
		 try {
			 List<Members> MemberList = new ArrayList<Members>();
			 
			 SQLiteDatabase db;
			 db = this.getReadableDatabase(); // Read Data
				
			 String strSQL = "SELECT  * FROM " +TABLE_DESPOINT;
			 Cursor cursor = db.rawQuery(strSQL, null);
			 
			 	if(cursor != null)
			 	{
			 	    if (cursor.moveToFirst()) {
			 	        do {
			 	        	Members member = new Members();
			 	        	member.sMemberID(cursor.getInt(0));
			 	        	member.sLat(cursor.getDouble(1));
			 	        	member.sLng(cursor.getDouble(2));
			 	        	member.sMobileno(cursor.getString(3));
			 	        	MemberList.add(member);
			 	        } while (cursor.moveToNext());
			 	    }
			 	}
			 	cursor.close();
				db.close();
				return MemberList;
				
		 } catch (Exception e) {
		    return null;
		 }

	}

}
